package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.model.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpuManageServiceImpl implements SpuManageService {

    @Autowired
    private SpuInfoService spuInfoService;
    @Autowired
    private SpuImageService spuImageService;
    @Autowired
    private SpuPosterService spuPosterService;
    @Autowired
    private SpuSaleAttrService spuSaleAttrService;
    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;
    @Autowired
    private BaseSaleAttrService baseSaleAttrService;

    /**
     * 分页查询商品spu列表
     * @param page
     * @param category3Id
     * @return
     */
    @Override
    public Page<SpuInfo> getSpuListByPage(Long page, Long size, Long category3Id) {
        Page<SpuInfo> spuInfoPage = new Page<>(page, size);
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuInfo::getCategory3Id,category3Id);
        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        return spuInfoService.page(spuInfoPage,queryWrapper);
    }

    /**
     * 获取销售属性列表
     * @return
     */
    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        return baseSaleAttrService.list();
    }

    /**
     * 保存商品spu信息
     * @param spuInfo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1.将商品基本信息封装为SpuInfo对象,存到spu_info表
        spuInfoService.save(spuInfo);
        Long spuId = spuInfo.getId();

        //2.将用户提交商品图片封装为SpuImage集合,存到spu_image表
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if(!CollectionUtils.isEmpty(spuImageList)){
            spuImageList.stream().forEach(spuImage -> {
                spuImage.setSpuId(spuId);
            });
            //2.1批量保存
            spuImageService.saveBatch(spuImageList);
        }

        //3.将用户提交海报照片封装为SpuPoster集合,存到spu_poster表
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if(!CollectionUtils.isEmpty(spuPosterList)){
            spuPosterList.stream().forEach(spuPoster -> {
                spuPoster.setSpuId(spuId);
            });
            //3.1批量保存
            spuPosterService.saveBatch(spuPosterList);
        }

        //4.将用户提交商品销售属性封装为SpuSaleAttrr集合,存到spu_sale_attr表
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if(!CollectionUtils.isEmpty(spuSaleAttrList)){
            spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                //关联商品
                spuSaleAttr.setSpuId(spuId);
                //5.将用户提交商品销售属性值封装为SpuSaleAttrValue集合,存到Spu_Sale_Attr_Value表
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if(!CollectionUtils.isEmpty(spuSaleAttrValueList)){
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        //5.1关联商品
                        spuSaleAttrValue.setSpuId(spuId);
                        //5.2为平台属性赋值
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                    });
                    //5.3批量保存
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }
            });
            //4.1批量保存
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }
    }

    /**
     * 查询spu图片集合
     * @param spuId
     * @return
     */
    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        LambdaQueryWrapper<SpuImage> queryWrapper = new LambdaQueryWrapper<SpuImage>();
        queryWrapper.eq(SpuImage::getSpuId,spuId);
        return spuImageService.list(queryWrapper);
    }

    /**
     * 根据SPUID查询海报列表
     * @param spuId
     * @return
     */
    @Override
    public List<SpuPoster> getSpuPosterBySpuId(long spuId) {
        LambdaQueryWrapper<SpuPoster> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuPoster::getSpuId,spuId);
        List<SpuPoster> spuPosterList = spuPosterService.list(queryWrapper);
        return spuPosterList;
    }

    /**
     * 根据SPU查询所有销售属性,用销售属性来判断当前商品是否被选中
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        //1.获取销售属性持久层的Mapper对象
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper)spuSaleAttrService.getBaseMapper();
        //调用自定义sql
        List<SpuSaleAttr> list = spuSaleAttrMapper.getSpuSaleAttrListBySku(skuId,spuId);
        return list;
    }

    /**
     * 查询每个商品SKU 销售属性和SKUID对照关系
     * @param spuId
     * @return
     */
    @Override
    public Map<String, String> getSkuValueIdsMap(Long spuId) {
        //1.创建销售属性的持久层?Mapper对象
        HashMap<String, String> map = new HashMap<String, String>();
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        List<Map<String,Object>> maps = spuSaleAttrMapper.getSkuValueIdsMap(spuId);
        if(!CollectionUtils.isEmpty(maps)){
            maps.stream().forEach(SkuSaleAttrValueMapper ->{
                String skuId = SkuSaleAttrValueMapper.get("sku_id").toString();
                String valueIds = SkuSaleAttrValueMapper.get("value_ids").toString();
                map.put(valueIds,skuId);
            });
        }
        return map;
    }
}